package com.repair.admin.user.service.impl;

import com.repair.admin.user.service.AdminUserService;
import com.repair.api.user.request.UserBlockRequest;
import com.repair.api.user.request.UserQueryPageRequest;
import com.repair.api.user.request.UserQueryRequest;
import com.repair.api.user.request.param.UserQueryParam;
import com.repair.api.user.response.UserQueryResponse;
import com.repair.api.user.response.data.UserInfo;
import com.repair.api.user.service.UserFacadeService;
import com.repair.base.exception.BizErrorCode;
import com.repair.base.exception.BizException;
import com.repair.base.response.PageResponse;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import java.util.List;

/**
 * @author chunyu
 * @since 2024/9/27,21:26
 */
@Service
public class AdminUserServiceImpl implements AdminUserService {

    @DubboReference
    private UserFacadeService userFacadeService;

    @Override
    public List<UserInfo> getAllUsers(UserQueryParam userQueryParam) {
        UserQueryPageRequest<UserQueryParam> queryParam = new UserQueryPageRequest<>();
        queryParam.setData(userQueryParam);
        queryParam.setCurrentPage(userQueryParam.getCurrentPage());
        queryParam.setPageSize(userQueryParam.getPageSize());
        PageResponse<UserInfo> pageResponse = userFacadeService.getAllUsers(queryParam);
        if (!pageResponse.getSuccess()) {
            throw new BizException(BizErrorCode.REMOTE_CALL_RESPONSE_IS_FAILED);
        }
        return pageResponse.getData();
    }

    @Override
    public UserInfo selectUsers(Long id) {
        UserQueryRequest request = new UserQueryRequest(id);
        UserQueryResponse<UserInfo> response = userFacadeService.query(request);
        if (!response.getSuccess()) {
            throw new BizException(BizErrorCode.REMOTE_CALL_RESPONSE_IS_FAILED);
        }
        return response.getData();
    }

    @Override
    public void updateState(Long id) {
        UserBlockRequest request = new UserBlockRequest(id);
        UserQueryResponse<Boolean> userQueryResponse = userFacadeService.blockUser(request);
        if (!userQueryResponse.getSuccess()) {
            throw new BizException(BizErrorCode.NOTICE_SAVE_FAILED);
        }
    }

    @Override
    public UserInfo selectBlockUser(Long id) {
        UserQueryRequest request = new UserQueryRequest(id);
        UserQueryResponse<UserInfo> blockResponse = userFacadeService.query(request);
        if (!blockResponse.getSuccess()) {
            throw new BizException(BizErrorCode.REMOTE_CALL_RESPONSE_IS_FAILED);
        }
        return blockResponse.getData();
    }
}
